<template>
  <div class="body-box album-body"  v-loading = "loading">
    <div v-if="load">
      <div v-if="!is_detail">
        <el-button
          class="com-btn add-album"
          size="mini"
          icon="el-icon-plus"
          @click="newAlbum()"
        >新建相册</el-button>
        <window-title title="相册列表"></window-title>
        <div class="album_empty" v-if="!album_list.length">
          <img
            src="https://ykp-new.oss-cn-hangzhou.aliyuncs.com/7b01bdfe3f80378002f7ca7c4e77ec3.png"
          />
          <div>暂无相册，请先创建</div>
        </div>
        <div class="album-list">
          <el-row :gutter="20">
            <el-col :span="6" v-for="(item,key) in album_list" :key="key">
              <el-card class="album-item" shadow="never">
                <el-image
                  fit="cover"
                  :src="item.cover  + '?x-oss-process=image/resize,h_300'"
                  class="album-cover"
                ></el-image>
                <div class="album-memo">
                  <div class="album-memo-top">
                    <div class="album-memo-name">{{item.name}}</div>
                    <i class="el-icon-delete" @click="deleteAlbum(key)"></i>
                  </div>
                  <div class="album-time">创建于：{{item.create_time}}</div>
                  <div class="album-see-btn" @click="seeAlbum(item.id)">查看相册</div>
                </div>
              </el-card>
            </el-col>
          </el-row>
        </div>
      </div>
      <div v-else>
        <album-detail @back="backJump()" :album_id = "album_id"></album-detail>
      </div>
    </div>
    <!-- 新建相册 -->
    <el-dialog :visible.sync="new_album" title="新建相册" :append-to-body="true" width="500px">
      <el-form size="small" label-position="top" label-width="80px">
        <el-form-item label="相册名">
          <el-input placeholder="请输入相册名" v-model="new_form.name" maxlength="6"></el-input>
        </el-form-item>
        <el-form-item label="封面">
          <div class="album-cover-box" v-if="!new_form.cover">
            <i class="el-icon-plus"></i>
            <input type="file" class="add-cover-file" @change="upCover($event)" />
          </div>
          <el-image
            :src="$oss_url + new_form.cover  + '?x-oss-process=image/resize,h_300'"
            fir="cover"
            class="add-cover-src"
            v-else
          ></el-image>
        </el-form-item>
        <el-form-item>
          <el-button class="com-btn" @click="addAlbum()">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import windowTitle from "../../../components/unit/title.vue";
import albumDetail from "./album-detail.vue";

export default {
  components: { windowTitle, albumDetail },
  props: { active_id: {} },
  data() {
    return {
      album_list: [],
      is_detail: false,
      new_album: false,
      new_form: { name: "", cover: "" },
      load: false,
      loading : true,
      album_id : 0,
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    //获取相册列表
    getData() {
      this.loading = true;
      this.ajaxs("album/albumList", {
        data: { active_id: this.active_id },
        success: res => {
          this.album_list = res.msg;
          this.load = true;
          this.loading = false;
        }
      });
    },
    //上传封面
    upCover(e) {
      var img = e.currentTarget.files[0];
      //获取文件后缀名
      var arr = img["name"].split(".");
      var img_type = arr[1];
      let OSS = require("ali-oss");
      let client = new OSS({
        region: "oss-cn-hangzhou",
        //云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，部署在服务端使用RAM子账号或STS，部署在客户端使用STS。
        accessKeyId: "LTAIWdxCuPiIzLfm",
        accessKeySecret: "z1zLih5CgWNkWfuzHj7SbpJKVdME2a",
        bucket: "ykp-new"
      });
      // client.options.endpoint.protocol = "https:";
      //获取当前时间
      var date = new Date();
      var file_name =
        date.getFullYear() +
        "/" +
        (date.getMonth() + 1) +
        "/" +
        date.getDate() +
        "/" +
        date.getHours() +
        "_" +
        date.getMinutes() +
        "_" +
        date.getSeconds() +
        "_" +
        date.getMilliseconds();
      let filename = "/active/cover/" + file_name + "." + img_type;
      let res_file = img;
      let that = this;
      async function put() {
        try {
          //object-name可以自定义为文件名（例如file.txt）或目录（例如abc/test/file.txt）的形式，实现将文件上传至当前Bucket或Bucket下的指定目录。
          let result = await client
            .multipartUpload(filename, res_file, {
              progress: percentage => {}
            })
            .then(res => {
              that.new_form.cover = res.name;
            })
            .catch(err => {
              return that.toast(0, "上传失败");
            });
        } catch (e) {
          return that.toast(0, "上传失败");
        }
      }
      put();
    },
    //删除相册
    deleteAlbum(key) {
      this.$confirm("确认删除该相册?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.ajaxs('album/deleteAlbum',{
            data : { id : this.album_list[key].id },
            success:(res) =>{
              this.toast(res.code,res.msg);
              if(res.code) {
                this.album_list.splice(key , 1);
              }
            }
          });
          this.album_list.splice(key, 1);
        })
        .catch(() => {
        });
    },
    //查看相册
    seeAlbum(id) {
      this.is_detail = true;
      this.album_id = id;
    },
    //返回列表
    backJump() {
      this.is_detail = false;
    },
    //新建相册
    newAlbum() {
      this.new_album = true;
    },
    //添加相册
    addAlbum() {
      if (!this.new_form.name) return this.toast(0, "请输入相册名称");
      if (!this.new_form.cover) return this.toast(0, "请上传封面");
      var data = this.new_form;
      data.active_id = this.active_id;
      this.ajaxs("album/addAlbum", {
        data: data,
        success: res => {
          this.toast(res.code, res.msg);
          if (res.code) {
            this.new_album = false;
            this.getData();
          }
        }
      });
    },
    //获取当前时间
    getNowTime() {
      var now_data = new Date();
      var Y = now_data.getFullYear();
      var M = now_data.getMonth() + 1;
      var D = now_data.getDate();
      var H = now_data.getHours();
      var I = now_data.getMinutes();
      var S = now_data.getSeconds();

      if (M < 10) M = "0" + M;
      if (D < 10) D = "0" + D;
      if (H < 10) H = "0" + H;
      if (I < 10) H = "0" + I;
      if (S < 10) H = "0" + S;
      return Y + "-" + M + "-" + D + " " + H + ":" + I + ":" + S;
    }
  }
};
</script>

<style>
.album-cover-box {
  width: 150px;
  height: 150px;
  border: 1px dashed #c1c1c1;
  border-radius: 3px;
  cursor: pointer;
  text-align: center;
  line-height: 150px;
  position: relative;
}
.album-cover-box i {
  font-size: 24px;
  line-height: 150px;
  color: #aaa;
}
.add-cover-file {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  opacity: 0;
  cursor: pointer;
}
.add-cover-src {
  width: 150px;
  height: 150px;
  border-radius: 3px;
}
.album-see-btn {
  width: calc(100% - 40px);
  height: 40px;
  line-height: 40px;
  text-align: center;
  border: 1px solid #fff;
  border-radius: 3px;
  margin-top: 30px;
  position: absolute;
  bottom: 20px;
}
.album-time {
  margin-top: 20px;
  font-size: 12px;
}
.album-memo-name {
  float: left;
  width: calc(100% - 30px);
  font-size: 22px;
  overflow: hidden;
}
.album-memo-top {
  overflow: hidden;
  height: 30px;
  line-height: 30px;
}
.album-memo-top > i {
  float: right;
  font-size: 16px;
  margin-top: 3px;
  border: 1px solid #fff;
  border-radius: 3px;
  padding: 3px 5px;
}
.album-cover {
  width: 188px;
  height: 188px;
}
.album-memo {
  position: absolute;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.8);
  top: 0;
  left: 0;
  opacity: 0;
  transition: all 0.2s linear;
  cursor: pointer;
  color: #fff;
  padding: 20px;
}
.album-item:hover .album-memo {
  opacity: 1;
}
.album-item {
  margin-bottom: 20px;
  position: relative;
}
.album-body {
  position: relative;
}
.add-album {
  position: absolute;
  right: 20px;
  z-index:9999;
}
.album_empty {
  margin-top: 20px;
  text-align: center;
}
.album_empty > img {
  width: 80px;
  display: block;
  margin: auto;
}
.album_empty > div {
  margin-top: 10px;
  color: #999;
}
.album-list {
  overflow: hidden;
  margin-top: 20px;
}
</style>